<?php

namespace App\Services\UserInviteInfo;

use App\Models\User;
use App\Models\Product;
use Illuminate\Support\Str;
use Intervention\Image\Gd\Font;
use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;

class MakeProductInvitePoster
{
	/**
	 * @var User
	 */
	protected $user;
	
	/**
	 * @var Product
	 */
	protected $product;
	
	protected $channel = 'wap';
	
	public function __construct(User $user, Product $product, $channel = 'wap')
	{
		$this->user = $user;
		$this->product = $product;
		$this->channel = $channel;
	}
	
	protected function getPosterName()
	{
		return "poster-user-{$this->user->id}-product-{$this->product->id}.png";
	}
	
	protected function getRemotePosterDir()
	{
		return 'images/mini-product-invite-posters';
	}
	
	protected function getRemotePosterSavePath()
	{
		return $this->getRemotePosterDir() . '/' . $this->getPosterName();
	}
	
	protected function getLocalPosterSavePath(string $name)
	{
		$localPath = public_path('temp/mini-product-invite-posters');
		if (! File::isDirectory($localPath)) {
			File::makeDirectory($localPath, 755, true, true);
		}
		return "{$localPath}/{$name}.png";
	}
	
	public function make()
	{
		$product = $this->product;
		
		if (Storage::exists($this->getRemotePosterSavePath())) {
			return Storage::url($this->getRemotePosterSavePath());
		}
		
		$posterDir = $this->getRemotePosterDir();
		$posterName = $this->getPosterName();
		
		$back = Image::make(oss('images/mini/product/product-poster-bg.png'));
		
		$avatar = Image::make(img_path($this->user->avatar))->resize(104, 104);
		
		$cover = Image::make(img_path($product->cover))->resize(582, 582);
		
		$vip = Image::make(oss('images/mini/product/price-vip-circle.png'));
		
		$finalPrice = big_decimal($product->price_lowest);
		
		// make qrcode.
		$qrcode = Image::make($this->user->inviteQrcode($product))->resize(150, 150);
		
		Image::canvas(750, 1334, '#FFFFFF')
			->insert($back, 'top', 0, 0)
			->insert($avatar, 'top-left', 84, 270)
			->text('邀你来逛有点东西，会员抄底价', 212, 318, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#000000');
				$font->size(28);
			})
			->text($this->user->name, 212, 360, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#999999');
				$font->size(28);
			})
			->insert($cover, 'top', 84, 398)
			->text(Str::limit($product->title, 35), 84, 1030, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->size(30);
			})
			->insert($vip, 'bottom-left', 84, 150)
			->text('￥', 215, 1180, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#FC4746');
				$font->size(40);
			})
			->text((string)$finalPrice, 255, 1185, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#FC4746');
				$font->size(64);
			})
			->text("市场价格 {$product->price_lowest}", 84, 1110, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#000000');
				$font->size(40);
			})
			->insert($qrcode, 'bottom-right', 70, 120)
			->save($this->getLocalPosterSavePath($posterName));
		
		// move to oss.
		Storage::putFileAs($posterDir, $this->getLocalPosterSavePath($posterName), $posterName);
		
		// remove local poster.
		File::delete($this->getLocalPosterSavePath($posterName));
		
		return Storage::url("{$posterDir}/{$posterName}");
	}
}
